 package org.portion.format;

/**
 * \u30bf\u30a4\u30c8\u30eb: \u6587\u5b57\u30b3\u30fc\u30c9\u5bfe\u5fdcBeans
 * \u8aac\u660e:
 * \u8457\u4f5c\u6a29:   Copyright (c) 2001
 * \u4f1a\u793e\u540d:   JMAS
 * @author  Ryo.Ota
  * @version 1.0
 */

 import java.util.*;

public class KanaConverter{

  public KanaConverter(){}

  public static String toZenkaku(String str){
      return KanaConvert(true, str);
  }

  public static String toHankaku(String str){
      return KanaConvert(false, str);
  }

  private static String KanaConvert(boolean sw, String str){
    Hashtable tb = getKanaTable(sw);
    String strRet = "";
    String src = "";
    boolean flg = false;
    String strNext = "";

    for(int i=0; i<str.length(); i++){

      if(sw){ //toZenkaku\u306e\u5834\u5408

        if(flg){    //"\uff9e" \u304b "\uff9f"\u304c\u3042\u3063\u305f\u3068\u304d
              flg = false;
              continue;   //\u30d5\u30e9\u30b0\u3092\u5bdd\u304b\u305b\u3066\u3001\u30eb\u30fc\u30d7\u3092\u4e00\u56de\u98db\u3070\u3059
        }

        try{
            strNext = str.substring(i+1, i+2);
        }catch(Exception e){}

        if( strNext.equals("\uff9e") || strNext.equals("\uff9f") ){
              src = str.substring(i, i+2);

              if( tb.containsKey(src) ){
                strRet = strRet + tb.get(src);
              }else{
                strRet = strRet + src;
              }

              flg = true;
        }else{
              if( tb.containsKey(str.substring(i, i+1)) ){
                strRet = strRet + tb.get(str.substring(i, i+1));
              }else{
                strRet = strRet + str.substring(i, i+1);
              }
        }

      }else{ //toHankaku\u306e\u5834\u5408
              //\u5909\u63db\u30c6\u30fc\u30d6\u30eb\u306b\u3042\u308c\u3070
              if( tb.containsKey(str.substring(i, i+1)) ){
                strRet = strRet + tb.get(str.substring(i, i+1));
              }else{
                strRet = strRet + str.substring(i, i+1);
              }
      }
    }
    return strRet;
  }

  private static Hashtable getKanaTable(boolean sw){
    Hashtable KanaTable = new Hashtable();

    String strZenkaku[] = {"\u30a2","\u30a4","\u30a6","\u30a8","\u30aa",
                           "\u30ab","\u30ad","\u30af","\u30b1","\u30b3",
                           "\u30b5","\u30b7","\u30b9","\u30bb","\u30bd",
                           "\u30bf","\u30c1","\u30c4","\u30c6","\u30c8",
                           "\u30ca","\u30cb","\u30cc","\u30cd","\u30ce",
                           "\u30cf","\u30d2","\u30d5","\u30d8","\u30db",
                           "\u30de","\u30df","\u30e0","\u30e1","\u30e2",
                           "\u30e4","\u30e6","\u30e8","\u30e3","\u30e5","\u30e7",
                           "\u30e9","\u30ea","\u30eb","\u30ec","\u30ed",
                           "\u30ef","\u30f2","\u30f3","\u30c3","\u30fc","\uff0f","\uff0d","\u30fb","\u3000",
                           "\u30a1","\u30a3","\u30a5","\u30a7","\u30a9",
                           "\u30ac","\u30ae","\u30b0","\u30b2","\u30b4",
                           "\u30b6","\u30b8","\u30ba","\u30bc","\u30be",
                           "\u30c0","\u30c2","\u30c5","\u30c7","\u30c9",
                           "\u30d0","\u30d3","\u30d6","\u30d9","\u30dc","\u30f4",
                           "\u30d1","\u30d4","\u30d7","\u30da","\u30dd",
                           "\uff21","\uff22","\uff23","\uff24","\uff25",
                           "\uff26","\uff27","\uff28","\uff29","\uff2a",
                           "\uff2b","\uff2c","\uff2d","\uff2e","\uff2f",
                           "\uff30","\uff31","\uff32","\uff33","\uff34",
                           "\uff35","\uff36","\uff37","\uff38","\uff39","\uff3a",
                           "\uff10","\uff11","\uff12","\uff13","\uff14",
                           "\uff15","\uff16","\uff17","\uff18","\uff19"};

    String strHankaku[] = {"\uff71","\uff72","\uff73","\uff74","\uff75",
                           "\uff76","\uff77","\uff78","\uff79","\uff7a",
                           "\uff7b","\uff7c","\uff7d","\uff7e","\uff7f",
                           "\uff80","\uff81","\uff82","\uff83","\uff84",
                           "\uff85","\uff86","\uff87","\uff88","\uff89",
                           "\uff8a","\uff8b","\uff8c","\uff8d","\uff8e",
                           "\uff8f","\uff90","\uff91","\uff92","\uff93",
                           "\uff94","\uff95","\uff96","\uff6c","\uff6d","\uff6e",
                           "\uff97","\uff98","\uff99","\uff9a","\uff9b",
                           "\uff9c","\uff66","\uff9d","\uff6f","\uff70","/","-","\uff65"," ",
                           "\uff67","\uff68","\uff69","\uff6a","\uff6b",
                           "\uff76\uff9e","\uff77\uff9e","\uff78\uff9e","\uff79\uff9e","\uff7a\uff9e",
                           "\uff7b\uff9e","\uff7c\uff9e","\uff7d\uff9e","\uff7e\uff9e","\uff7f\uff9e",
                           "\uff80\uff9e","\uff81\uff9e","\uff82\uff9e","\uff83\uff9e","\uff84\uff9e",
                           "\uff8a\uff9e","\uff8b\uff9e","\uff8c\uff9e","\uff8d\uff9e","\uff8e\uff9e", "\uff73\uff9e",
                           "\uff8a\uff9f","\uff8b\uff9f","\uff8c\uff9f","\uff8d\uff9f","\uff8e\uff9f",
                           "A","B","C","D","E",
                           "F","G","H","I","J",
                           "K","L","M","N","O",
                           "P","Q","R","S","T",
                           "U","V","W","X","Y","Z",
                           "0","1","2","3","4",
                           "5","6","7","8","9"};

    if(sw){
      for(int i=0; i<strZenkaku.length; i++){
        KanaTable.put(strHankaku[i], strZenkaku[i]);
      }
    }else{
      for(int i=0; i<strZenkaku.length; i++){
        KanaTable.put(strZenkaku[i], strHankaku[i]);
      }
    }

    return KanaTable;
  }

  private Hashtable getEncodedKanaTable(boolean sw){
    Hashtable EncodedTable = new Hashtable();

    String tbZenkaku[] = {"%83%41","%83%43","%83%45","%83%47","%83%49", //\u30a2 4
                          "%83%4A","%83%4C","%83%4E","%83%50","%83%52", //\u30ab 9
                          "%83%54","%83%56","%83%58","%83%5A","%83%5C", //\u30b5 14
                          "%83%5E","%83%60","%83%63","%83%65","%83%67", //\u30bf 19
                          "%83%69","%83%6A","%83%6B","%83%6C","%83%6D", //\u30ca 24
                          "%83%6E","%83%71","%83%74","%83%77","%83%7A", //\u30cf 29
                          "%83%7D","%83%7E","%83%80","%83%81","%83%82", //\u30de 34
                          "%83%84","%83%86","%83%88",                   //\u30e4 37
                          "%83%89","%83%8A","%83%8B","%83%8C","%83%8D", //\u30e9 42
                          "%83%8F","%83%92","%83%93","%83%62",          //\u30ef\u30c3 46
                          "%83%40","%83%42","%83%44","%83%46","%83%48", //\u30a1-\u30a9 51
                          "%83%4B","%83%4D","%83%4F","%83%51","%83%53", //\u30ac
                          "%83%55","%83%57","%83%59","%83%5B","%83%5D", //\u30b6
                          "%83%5F","%83%61","%83%64","%83%66","%83%68", //\u30c0
                          "%83%6F","%83%72","%83%75","%83%78","%83%7B","%83%94", //\u30d0\u30fb\u30f4
                          "%83%70","%83%73","%83%76","%83%79","%83%7C"}; // \u30d1

    String tbHankaku[] = {"%B1","%B2","%B3","%B4","%B5", //\u30a2 4
                          "%B6","%B7","%B8","%B9","%BA", //\u30ab 9
                          "%BB","%BC","%BD","%BE","%BF", //\u30b5 14
                          "%C0","%C1","%C2","%C3","%C4", //\u30bf 19
                          "%C5","%C6","%C7","%C8","%C9", //\u30ca 24
                          "%CA","%CB","%CC","%CD","%CE", //\u30cf 29
                          "%CF","%D0","%D1","%D2","%D3", //\u30de 34
                          "%D4","%D5","%D6",             //\u30e4 37
                          "%D7","%D8","%D9","%DA","%DB", //\u30e9 42
                          "%DC","%A6","%DD","%AF",       //\u30ef\u30c3 46
                          "%A7","%A8","%A9","%AA","%AB", //\u30a1-\u30a9 51
                          "%B6%DE","%B7%DE","%B8%DE","%B9%DE","%BA%DE", //\u30ac 9
                          "%BB%DE","%BC%DE","%BD%DE","%BE%DE","%BF%DE", //\u30b6 14
                          "%C0%DE","%C1%DE","%C2%DE","%C3%DE","%C4%DE", //\u30c0 19
                          "%CA%DE","%CB%DE","%CC%DE","%CD%DE","%CE%DE","%B3%DE", //\u30d0\u30fb\u30f4
                          "%CA%DF","%CB%DF","%CC%DF","%CD%DF","%CE%DF"}; // \u30d1
    if(sw){
      for(int i=0; i<tbZenkaku.length; i++){
        EncodedTable.put(tbZenkaku[i], tbHankaku[i]);
      }
    }else{
      for(int i=0; i<tbZenkaku.length; i++){
        EncodedTable.put(tbHankaku[i], tbZenkaku[i]);
      }
    }
    return EncodedTable;
  }
}
